﻿using soma.Models;
using soma.Models.Resources;
using soma.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace soma.Controllers
{
    public class PhotosController : ApiController
    {
        // GET api/photos
        public List<PhotoResource> Get()
        {
            return new List<PhotoResource>();
        }

        // GET api/photos/5
        public List<PhotoResource> Get(string tags, int results, string key, bool useSynonyms)
        {
            List<PhotoResource> photoResources = new List<PhotoResource>();

            string errorMessage;
            if (APIKeyVerifier.Instance.VerifyAPIKeyValidity(key, out errorMessage))
            {
                List<Tag> tagList = TagsSplitter.SplitAndDistributeTags(tags, results);

                if (useSynonyms)
                {
                    // Get synonyms 
                    foreach (Tag tag in tagList)
                    {
                        int noSynonyms = (int)Math.Sqrt(tag.NumberResults);

                        if (noSynonyms == 0)
                        {
                            noSynonyms = 1;
                        }

                        List<string> synonymsForTag = SynonymsProvider.Instance.ProvideSynonyms(tag.TagName, noSynonyms);
                        foreach (string synonym in synonymsForTag)
                        {
                            tag.Synonyms.Add(new Tag(synonym, 0));
                        }

                    }
                    // Devide results between tags
                    foreach (Tag tag in tagList)
                    {
                        TagsSplitter.DivideResultBetweenTagAndSynonyms(tag);
                    }
                }
                foreach (Tag tag in tagList)
                {
                    photoResources.AddRange(PhotosRepository.Instance.GetResourceByTag(tag.TagName, tag.NumberResults).Cast<PhotoResource>().ToList());
                    foreach (Tag synonyms in tag.Synonyms)
                    {
                        photoResources.AddRange(PhotosRepository.Instance.GetResourceByTag(synonyms.TagName, synonyms.NumberResults).Cast<PhotoResource>().ToList());
                    }
                }
            }

            return photoResources;
        }

        public HttpResponseMessage DeletePhoto(string tag, string id, string key)
        {
            HttpResponseMessage responseMessage = new HttpResponseMessage();

            string errorMessage;
            if (!APIKeyVerifier.Instance.VerifyAPIKeyValidity(key, out errorMessage))
            {
                responseMessage.StatusCode = HttpStatusCode.Forbidden;
                responseMessage.ReasonPhrase = errorMessage;

                return responseMessage;
            }

            bool deleteResult = PhotosRepository.Instance.DeleteResource(tag, id);

            if (!deleteResult)
            {
                responseMessage.StatusCode = HttpStatusCode.NotFound;
            }
            else
            {
                responseMessage.StatusCode = HttpStatusCode.OK;
            }

            return responseMessage;
        }
    }
}
